package newcrunch.gui.tree;

import java.awt.Component;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import jsyntaxpane.util.JarServiceProvider;

/**
 * @author WL
 */
public class TreeCellDisplay extends DefaultTreeCellRenderer
{
    private static ImageIcon classIcon;
    private static ImageIcon interfaceIcon;
    private static ImageIcon enumIcon;
    private static ImageIcon annotationIcon;
    private static ImageIcon enumConstIcon;
    private static ImageIcon privateFieldIcon;
    private static ImageIcon protectedFieldIcon;
    private static ImageIcon publicFieldIcon;
    private static ImageIcon packageFieldIcon;
    private static ImageIcon privateMethodIcon;
    private static ImageIcon protectedMethodIcon;
    private static ImageIcon packageMethodIcon;
    private static ImageIcon publicMethodIcon;
    
    static {
        Properties props = new Properties();
        InputStream stream = null;
        try {
            stream = JarServiceProvider.findResource("newcrunch/images/images.properties");
            props.load(stream);
            
            classIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("classIcon"))));
            interfaceIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("interfaceIcon"))));
            enumIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("enumIcon"))));
            annotationIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("annotationIcon"))));
            
            enumConstIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("enumConstIcon"))));
            
            privateFieldIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("privateField"))));
            protectedFieldIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("protectedField"))));
            publicFieldIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("publicField"))));
            packageFieldIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("packageField"))));
            
            privateMethodIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("privateMethod"))));
            protectedMethodIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("protectedMethod"))));
            packageMethodIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("packageMethod"))));
            publicMethodIcon = new ImageIcon(ImageIO.read(JarServiceProvider.findResource(props.getProperty("publicMethod"))));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null)
            {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public Component getTreeCellRendererComponent(
                        JTree tree,
                        Object value,
                        boolean sel,
                        boolean expanded,
                        boolean leaf,
                        int row,
                        boolean hasFocus) {

        super.getTreeCellRendererComponent(
                        tree, value, sel,
                        expanded, leaf, row,
                        hasFocus);
        
        if (!(value instanceof CustomTreeNode))
            return this;
        
        CustomTreeNode node = (CustomTreeNode)value;
        
        setIcon(null);
        switch (node.getType())
        {
            case INTERFACE:
                setIcon(interfaceIcon);
                break;
            case CLASS:
                setIcon(classIcon);
                break;
            case ENUM:
                setIcon(enumIcon);
                break;
            case ANNOTATION:
                setIcon(annotationIcon);
                break;
            case ENUM_CONST:
                setIcon(enumConstIcon);
                break;
            case PRIVATE_FIELD:
                setIcon(privateFieldIcon);
                break;
            case PROTECTED_FIELD:
                setIcon(protectedFieldIcon);
                break;
            case PUBLIC_FIELD:
                setIcon(publicFieldIcon);
                break;
            case PACKAGE_FIELD:
                setIcon(packageFieldIcon);
                break;
            case PACKAGE_METHOD:
                setIcon(packageMethodIcon);
                break;
            case PUBLIC_METHOD:
                setIcon(publicMethodIcon);
                break;
            case PRIVATE_METHOD:
                setIcon(privateMethodIcon);
                break;
            case PROTECTED_METHOD:
                setIcon(protectedMethodIcon);
                break;
        }
        
        this.setToolTipText(node.getToolTip());
        return this;
    }
}
